package org.anyin.gitee.shiro.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.shiro.base.BusinessCodeEnum;
import org.anyin.gitee.shiro.model.UserInfo;
import org.anyin.gitee.shiro.service.ILoginService;
import org.anyin.gitee.shiro.service.ITokenService;
import org.anyin.gitee.shiro.service.IUserInfoService;
import org.anyin.gitee.shiro.shiro.ShiroToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ITokenService tokenService;

    @Override
    public String login(String username, String password) {
        UserInfo userInfo = userInfoService.findByUsername(username);
        if(!userInfo.getPassword().equals(password)){
            throw BusinessCodeEnum.PWD_NOT_MATCH.getException();
        }
        // shiro 框架登录
        ShiroToken token = tokenService.createToken(userInfo);
        SecurityUtils.getSubject().login(token);

        return (String) token.getCredentials();
    }

    @Override
    public void logout(String token) {

    }
}
